﻿using FalionForm.ComAdd;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Util.Controls;
using VisionConfig;

namespace FalionForm
{
    /// <summary>
    /// Page3.xaml 的交互逻辑
    /// </summary>
    public partial class Page3 : Page
    {
        public static HWindows _dp = new HWindows();
        public Page3()
        {
            InitializeComponent();
            Display.Child = _dp;
            _dp.HL.dispTarg += disme;
            _dp.HL.MouseMv += HL_MouseMv;

            addpage.Navigate(ca1);//默认显示第一个界面
        }
        //数据定义
        public static bool dis = false;
        public static HObject Region_all = new HObject();
        public static int MousePointX, MousePointY;
        public static HObject ho_Z = new HObject(), ho_Y = new HObject(), ho_X = new HObject();

        ComAdd_1 ca1 = new ComAdd_1();
        ComAdd_2 ca2 = new ComAdd_2();
        ComAdd_3 ca3 = new ComAdd_3();
        ComAdd_4 ca4 = new ComAdd_4();

        //委托
        private void HL_MouseMv(int x, int y)//鼠标移动获取图片信息
        {
            MousePointX = x;
            MousePointY = y;
            this.Dispatcher.Invoke(new Action(() =>
            {
                labela.Content = "像素坐标：" + x.ToString() + "*" + y.ToString();
                if (x > 0 && y > 0)
                {
                    try
                    {
                        HTuple gx, gy, gz;
                        HOperatorSet.GetGrayval(ho_X, x, y, out gx);
                        HOperatorSet.GetGrayval(ho_Y, x, y, out gy);
                        HOperatorSet.GetGrayval(ho_Z, x, y, out gz);
                        labelb.Content = "X: " + gx.D.ToString("f2") + "\r\n" + "Y: " + gy.D.ToString("f2") + "\r\n" + "Z: " + gz.D.ToString("f2");
                    }
                    catch
                    {
                    }
                }
            }));       
         }
        void disme(HWindow HalconW)//显示ROI
        {
            if (dis)
            {
                if (Region_all != null && Region_all.IsInitialized())
                {
                    HalconW.SetColor("blue");
                    HOperatorSet.DispRegion(Region_all, HalconW);
                }
            }
        }


        //按钮
        string flg = "";
        private void Btn_head(object sender, RoutedEventArgs e)//菜单栏按钮
        {
            FButton fb = (FButton)sender;
            flg = fb.Name;
            switch (fb.Name)
            {
                case "btn1":
                    addpage.Navigate(ca1);
                    flg = "btn1";
                    op1.IsEnabled = true;
                    //结束显示线程
                    if (MainPage.pro.tdisplay != null && MainPage.pro.tdisplay.IsAlive)
                    {
                        MainPage.pro.Vis3D.bBreak = true;
                        MainPage.pro.tdisplay.Join();
                    }
                    break;
                case "btn2":
                    op.IsEnabled = true;
                    flg = "btn2";
                    addpage.Navigate(ca2);

                    if (ca1.tdisplay != null && ca1.tdisplay.IsAlive)
                    {
                        ca1.Vis3D.bBreak = true;
                    }
                    break;
                case "btn3":
                    addpage.Navigate(ca3);
                    flg = "btn1";
                    op1.IsEnabled = true;
                    break;
                case "btn4":
                    addpage.Navigate(ca4);
                    break;
                case "btn5":
                    break;
                case "btn6":
                    break;
                default:
                    break;

            }
            if (flg != "btn2")
            {
                op.IsEnabled = false;
                _dp.HW.ClearWindow();
            }
            if (flg != "btn1")
            {
                op1.IsEnabled = false;
                _dp.HW.ClearWindow();
            }
        }
        private void Btn_GetPicture(object sender, RoutedEventArgs e)//预处理图片
        {
            ca1.Clear();
            if (ho_X != null && ho_X.IsInitialized())
            {
                ho_X.Dispose();
                ho_Y.Dispose();
                ho_Z.Dispose();
            }
            try
            {
                HObject Image3;
                HOperatorSet.ReadImage(out Image3, "./" + "Pictures" + "/DColor");
                HOperatorSet.Decompose3(Image3, out ho_X, out ho_Y, out ho_Z);
                Image3.Dispose();
            }
            catch
            {

                MessageBox.Show("图片不存在");
                return;
            }
            if (Region_all != null && Region_all.IsInitialized())
            {
                Region_all.Dispose();
            }
           // ca2.Clear();
            dis = true;
            HOperatorSet.SetLut(_dp.HW, "default");
            _dp.SetImagePart(ho_Z);
            _dp.DramModel = false;
        }
        private void Btn_GetPictureModel(object sender, RoutedEventArgs e)//预处理图片
        {
            ca1.Clear();
            if (ho_X != null && ho_X.IsInitialized())
            {
                ho_X.Dispose();
                ho_Y.Dispose();
                ho_Z.Dispose();
            }
            try
            {
                HOperatorSet.ReadImage(out ho_X, "./" + "PicturesModel" + "/Ca_ho_X");
                HOperatorSet.ReadImage(out ho_Y, "./" + "PicturesModel" + "/Ca_ho_Y");
                HOperatorSet.ReadImage(out ho_Z, "./" + "PicturesModel" + "/Ca_ho_Z");
            }
            catch
            {

                MessageBox.Show("图片不存在");
                return;
            }
            if (Region_all != null && Region_all.IsInitialized())
            {
                Region_all.Dispose();
            }
            // ca2.Clear();
            dis = true;
            HOperatorSet.SetLut(_dp.HW, "default");
            _dp.SetImagePart(ho_Z);
            _dp.DramModel = false;
        }
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {

        }
        private void Btn_OpenPicture(object sender, RoutedEventArgs e)//读取图片
        {
            if (ho_X != null && ho_X.IsInitialized())
            {
                ho_X.Dispose();
                ho_Y.Dispose();
                ho_Z.Dispose();
            }
            try
            {
                HOperatorSet.ReadImage(out ho_X, "./Pictures" + "Ca_ho_X");
                HOperatorSet.ReadImage(out ho_Y, "./Pictures" + "Ca_ho_Y");
                HOperatorSet.ReadImage(out ho_Z, "./Pictures" + "Ca_ho_Z");

            }
            catch
            {
                MessageBox.Show("图片不存在");
                return;
            }
           // ca2.Clear();
            dis = false;
            HOperatorSet.SetLut(_dp.HW, "rainbow");
            _dp.SetImagePart(ho_Z);
            _dp.DramModel = false;
        }
        private void Btn_MaxPicture(object sender, RoutedEventArgs e)//图片最大化
        {
            //ca2.Clear();
            _dp.DramModel = false;
            _dp.SetImagePart();
        }
    }
}
    
